/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TestCombos.java
 *
 * Created on 04-mar-2012, 18:59:06
 */

package TestCombos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

/**
 *
 * @author heril
 */
public class TestCombos extends javax.swing.JFrame {

    /** Creates new form TestCombos */
    public TestCombos() {
        initComponents();
    }

    private void cargarCmb (JComboBox cmb, LinkedList items) {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for (int i = 0; i < items.size(); i++) {
            model.addElement(items.get(i));
        }
        cmb.setModel(model);
        cmb.setSelectedIndex(-1);
        cmb.setSelectedIndex(0);
    }

    //conexion a bd

    public static final String DRIVER = "oracle.jdbc.OracleDriver";
    public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:xe";

    /**
     * Crea una conexi�n JDBC para Oracle
     * @return una referencia al objeto Connection que representa la conexi�n abierta
     * contra el motor Oracle
     */

    private Connection createConnection() {
        Connection con = null;
        try {
            Class.forName(DRIVER);
            con = DriverManager.getConnection(DBURL, "mlaboral", "mlaboral");
        } catch (Exception e) {
            System.out.println("Error al abrir la BD (ORACLE): " + e.getMessage());
        }
        return con;
    }

    private LinkedList capitulos() {
        Connection con = this.createConnection();
        LinkedList res = new LinkedList();
        try {
            PreparedStatement st = con.prepareStatement("select id_capitulo,capitulo from enfermedades_ampliada group by id_capitulo, capitulo");
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Capitulo aux = new Capitulo();
                aux.setId(rs.getString(1));
                aux.setCapitulo(rs.getString(2));
                res.add(aux);
            }
            con.close();
        } catch (SQLException ex) {
            System.out.println("Capitulo "+ex.getMessage());
        } catch (Exception ex) {
        }
        
        return res;
    }

    private LinkedList contenidos(Capitulo id_cap) {
        Connection con = this.createConnection();
        LinkedList res = new LinkedList();
        try {
            PreparedStatement st = con.prepareStatement("select id_contenido,contenido from enfermedades_ampliada where " +
                    "id_capitulo = '" + id_cap.getId() + "' group by id_contenido, contenido");
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Contenido aux = new Contenido();
                aux.setId(rs.getString(1));
                aux.setCont(rs.getString(2));
                res.add(aux);
            }
            con.close();
        } catch (SQLException ex) {
            System.out.println("contenido "+ex.getMessage());
        } catch (Exception ex) {
        }
        return res;
    }

    private LinkedList enfermedades(Contenido id_cont) {
        Connection con = this.createConnection();
        LinkedList res = new LinkedList();
        try {
            PreparedStatement st = con.prepareStatement("select id_enfermedad,enfermedad from enfermedades_ampliada where " +
                    "id_contenido = '" + id_cont.getId() + "' group by id_enfermedad, enfermedad");
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Enfermedad aux = new Enfermedad();
                aux.setId(rs.getString(1));
                aux.setEnf(rs.getString(2));
                res.add(aux);
            }
            con.close();
        } catch (SQLException ex) {
            System.out.println(" enfer "+ex.getMessage());
        } catch (Exception ex) {
        }
        return res;
    }

    private LinkedList subEnfermedades(Enfermedad id_enf) {
        Connection con = this.createConnection();
        LinkedList res = new LinkedList();
        try {
            PreparedStatement st = con.prepareStatement("select id_subclasificacion,subclasificacion from enfermedades_ampliada where " +
                    "id_enfermedad = '" + id_enf.getId() + "' group by id_subclasificacion, subclasificacion");
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Enfermedad aux = new Enfermedad();
                aux.setId(rs.getString(1));
                aux.setEnf(rs.getString(2));
                res.add(aux);
            }
            con.close();
        } catch (SQLException ex) {
            System.out.println(" sub "+ex.getMessage());
        } catch (Exception ex) {
        }
        return res;
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        capitulo = new javax.swing.JComboBox();
        contenido = new javax.swing.JComboBox();
        enfermedad = new javax.swing.JComboBox();
        subenf = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        capitulo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        capitulo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                capituloItemStateChanged(evt);
            }
        });

        contenido.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        contenido.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                contenidoItemStateChanged(evt);
            }
        });

        enfermedad.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        enfermedad.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                enfermedadItemStateChanged(evt);
            }
        });

        subenf.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jButton1.setText("Cargar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(contenido, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(capitulo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(subenf, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(enfermedad, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(capitulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(contenido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(enfermedad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(subenf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        cargarCmb(capitulo, capitulos());
    }//GEN-LAST:event_jButton1ActionPerformed

    private void capituloItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_capituloItemStateChanged
        if (capitulo.getSelectedIndex() != -1) cargarCmb(contenido, contenidos((Capitulo)capitulo.getSelectedItem()));
    }//GEN-LAST:event_capituloItemStateChanged

    private void contenidoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_contenidoItemStateChanged
        if (contenido.getSelectedIndex() != -1) cargarCmb(enfermedad, enfermedades((Contenido)contenido.getSelectedItem()));
    }//GEN-LAST:event_contenidoItemStateChanged

    private void enfermedadItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_enfermedadItemStateChanged
        if (enfermedad.getSelectedIndex() != -1) cargarCmb(subenf, subEnfermedades((Enfermedad)enfermedad.getSelectedItem()));
    }//GEN-LAST:event_enfermedadItemStateChanged

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestCombos().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox capitulo;
    private javax.swing.JComboBox contenido;
    private javax.swing.JComboBox enfermedad;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JComboBox subenf;
    // End of variables declaration//GEN-END:variables

}
